from flask import Blueprint, request, jsonify
import time
from db import db
from utils.get_data import get_data
from utils.acl import acl
from utils.jwt_token import myjwt

bp_user = Blueprint("user", __name__)


@bp_user.route('/login', methods=['POST'])
def login():
    data = get_data()
    # 账号密码登录
    name = data['name']
    # 判断账号密码是否正确
    sql = "select * from user where name='%s'" % (str(name))
    res = db.find(sql)
    print(res)
    if not res:
        return jsonify({'code': 10010, 'meg': '账号或密码错误'})
    elif data['pwd'] == res['password']:
        start_time = time.time()
        data1 = {'data': {'name': data['name'],
                          'userid': res['id'],
                          'iat': start_time,
                          'exp': 30
                          }}
        # 生成token
        # 获取页面权限，接口权限
        token = myjwt.jwt_encode(data1)
        url_list = acl.get_page_reslist(res['id'])
        print(url_list)
        return jsonify({'code': 10011,
                        'token': token,
                        'userid': res['id'],
                        'username': res['name'],
                        'url_list': url_list})
